package com.byxy.mysystem.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.byxy.mysystem.model.system.SysRole;
import com.byxy.mysystem.service.system.ISysRoleService;
import com.byxy.mysystem.util.JsonResult;
import com.byxy.mysystem.util.ResultTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户角色表 前端控制器
 * </p>
 *
 * @author omd
 * @since 2022-10-04
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @PostMapping("/findByPage")
    public JsonResult<IPage<SysRole>> findByPage(@RequestBody Map<String,Object> params){
        long current = params.get("current") != null?Integer.valueOf(params.get("current").toString()):1;
        long size = params.get("size") != null?Integer.valueOf(params.get("size").toString()):10;
        IPage page = new Page(current,size);
        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        if(params.get("userName") != null && StringUtils.isNotBlank(params.get("userName").toString())){
            wrapper.eq("userName",params.get("userName").toString());
        }
        IPage<SysRole> result = sysRoleService.page(page, wrapper);
        return ResultTool.success(result);
    }

    @PostMapping("/find")
    public JsonResult<SysRole> find(Integer id){
        SysRole sysRole = sysRoleService.getById(id);
        return ResultTool.success(sysRole);
    }

    @PostMapping("/update")
    public JsonResult update(@RequestBody SysRole sysRole){
        boolean flag = sysRoleService.updateById(sysRole);
        return ResultTool.success(flag);
    }

    @PostMapping("/save")
    public JsonResult save(@RequestBody SysRole sysRole){
        boolean flag = sysRoleService.save(sysRole);
        return ResultTool.success(flag);
    }

    @PostMapping("/delete")
    public JsonResult save(List<Integer> ids){
        boolean flag = sysRoleService.removeByIds(ids);
        return ResultTool.success(flag);
    }
}
